package com.cenwangkeji.resident.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.BasicInfoBean;
import com.cenwangkeji.resident.model.bean.ServicepackageEvaluateBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.adapter.ServicePAckageEvaluateAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//服务包评价
public class ServicePackageEvaluateActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener{

    @BindView(R.id.rv)
    RecyclerView mRv;
    private ServicePAckageEvaluateAdapter mAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_service_package_evaluate;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRecyclerView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getDate();
    }

    private void getDate(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<ServicepackageEvaluateBean>() {
            @Override
            public void subscribe(ObservableEmitter<ServicepackageEvaluateBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put( Constant.token, User.getToken());
                map.put( "userId",User.getUserId());
                map.put( "mark",3);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.service_package_evaluate_list);
                Gson gson = new Gson();
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, ServicepackageEvaluateBean.class));
            }
        }).compose(RxUtils.<ServicepackageEvaluateBean>io_main()).subscribe(new ObserverOnNext<ServicepackageEvaluateBean>() {
            @Override
            public void onNext(ServicepackageEvaluateBean servicepackageEvaluateBean) {
                        if (servicepackageEvaluateBean.getMsg().equals(Constant.token_expired)){
                            signIn();
                            finish();
                        }else {
                            if (servicepackageEvaluateBean.getData()!=null && servicepackageEvaluateBean.getData().size()>0){
                                mAdapter.setNewData(servicepackageEvaluateBean.getData());
                            }else {
                                View emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
                                mAdapter.setEmptyView(emptyView);
                                MyToast.show(servicepackageEvaluateBean.getMsg());
                            }
                        }
                        dismissLoading();
            }
        });
    }

    private void initRecyclerView(){
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ServicePAckageEvaluateAdapter(null);
        mAdapter.setOnItemClickListener(this);
        mRv.setAdapter(mAdapter);

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent= new Intent(this,ServicePackageEvaluateDetailActivity.class);
        intent.putExtra("id", mAdapter.getData().get(position).getId());//评价id
        intent.putExtra("dzbId", mAdapter.getData().get(position).getDzbId());//服务包id
        startActivity(intent);
    }
}
